using MicroMsg.Common.Utils;
using MicroMsg.UI.Page;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MicroMsg.UI.Util
{
	public class ModeDialogHelper
	{
		private const string TAG = "ModeDialogHelper";
		private Popup mPopup;
		private PhoneApplicationPage mPage;
		private IApplicationBar mAppBar;
		private PhoneApplicationFrame mRootVisual;
		private Brush mBrushBackGround;
		private Storyboard mDlgShowStoryboard;
		private bool mIsMenuEnabled;
		private readonly List<bool> mBarButtonEnables = new List<bool>();
		private bool mIsShow;
		private Panel mOverlay;
		private UserControl mCtrl;
		private bool mIsEnableBackKeyPress = true;
		private bool mIsEnableBackKeyPressDestoryRootPage = true;
		public DlgReturnEvent DlgReturnCallBack;
		private bool mIsPopupLoaded;
		private void InitializeRootVisual()
		{
			if (this.mRootVisual == null)
			{
				this.mRootVisual = (Application.get_Current().get_RootVisual() as PhoneApplicationFrame);
			}
			if (this.mPage == null)
			{
				this.mPage = (this.mRootVisual.get_Content() as PhoneApplicationPage);
				if (this.mPage != null)
				{
					Log.smethod_5("ModeDialogHelper", "mPage.BackKeyPress += new EventHandler<CancelEventArgs>(mPage_BackKeyPress)");
					this.mPage.add_BackKeyPress(new EventHandler<CancelEventArgs>(this.mPage_BackKeyPress));
					if (this.mPage == MainPage.mainPageInstance)
					{
						MainPage.mainPageInstance.mNBackKeyRelayCount++;
					}
				}
			}
		}
		private void mPage_BackKeyPress(object sender, CancelEventArgs e)
		{
			e.set_Cancel(true);
			Log.smethod_5("ModeDialogHelper", "mPage_BackKeyPress");
			if (!this.mIsPopupLoaded)
			{
				Log.smethod_5("ModeDialogHelper", "mIsPopupLoaded==false");
				return;
			}
			if (this.mIsEnableBackKeyPress)
			{
				Log.smethod_5("ModeDialogHelper", "Hide DlgReturnEventArg");
				this.Hide(new DlgReturnEventArg(Dlg_Result.Dlg_BackKey_Result));
			}
			if (this.mIsEnableBackKeyPressDestoryRootPage)
			{
				BasePage basePage = this.mRootVisual.get_Content() as BasePage;
				if (basePage != null)
				{
					basePage.GoBack();
				}
			}
		}
		public ModeDialogHelper(UserControl userControl, bool isEnableBackKeyPress = true, Brush brushBackGround = null, bool isBackKeyDestoryRootPage = false)
		{
			this.mCtrl = userControl;
			this.mIsEnableBackKeyPress = isEnableBackKeyPress;
			this.mBrushBackGround = R.theme.COMMON_MSGBOX_COVER_BRUSH;
			this.mIsEnableBackKeyPressDestoryRootPage = isBackKeyDestoryRootPage;
		}
		public void Show()
		{
			if (this.mIsShow)
			{
				return;
			}
			this.InitializeRootVisual();
			Canvas canvas = new Canvas();
			canvas.set_Background(new SolidColorBrush(Colors.get_Blue()));
			this.mOverlay = canvas;
			this.mOverlay.get_Children().Add(this.mCtrl);
			bool flag;
			double width = (flag = (1 == (1 & this.mRootVisual.get_Orientation()))) ? this.mRootVisual.get_ActualWidth() : this.mRootVisual.get_ActualHeight();
			double height = flag ? this.mRootVisual.get_ActualHeight() : this.mRootVisual.get_ActualWidth();
			if (this.mBrushBackGround == null)
			{
				this.mBrushBackGround = new SolidColorBrush(Color.FromArgb(120, 0, 0, 0));
			}
			Rectangle rectangle = new Rectangle();
			rectangle.set_Width(width);
			rectangle.set_Height(height);
			rectangle.set_Fill(this.mBrushBackGround);
			UIElement uIElement = rectangle;
			this.mOverlay.get_Children().Insert(0, uIElement);
			this.mBarButtonEnables.Clear();
			if (this.mPage != null && this.mPage.get_ApplicationBar() != null)
			{
				this.mAppBar = this.mPage.get_ApplicationBar();
				this.mIsMenuEnabled = this.mAppBar.get_IsMenuEnabled();
				this.mAppBar.set_IsMenuEnabled(false);
				IEnumerator enumerator = this.mAppBar.get_Buttons().GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						object current = enumerator.get_Current();
						ApplicationBarIconButton applicationBarIconButton = current as ApplicationBarIconButton;
						if (applicationBarIconButton != null)
						{
							this.mBarButtonEnables.Add(applicationBarIconButton.get_IsEnabled());
							applicationBarIconButton.set_IsEnabled(false);
						}
					}
				}
				finally
				{
					IDisposable disposable = enumerator as IDisposable;
					if (disposable != null)
					{
						disposable.Dispose();
					}
				}
			}
			Popup popup = new Popup();
			popup.set_Child(this.mOverlay);
			this.mPopup = popup;
			this.mPopup.add_Loaded(new RoutedEventHandler(this.mPopup_Loaded));
			this.mPopup.add_Unloaded(new RoutedEventHandler(this.mPopup_Unloaded));
			this.mPopup.set_IsOpen(true);
			this.mDlgShowStoryboard = StoryBoardHelper.StartStory("DlgShowAni", this.mCtrl);
			this.mIsShow = true;
			BasePage.setSystemTrayColorInDialog();
		}
		private void mPopup_Unloaded(object sender, RoutedEventArgs e)
		{
			this.mIsPopupLoaded = false;
		}
		private void mPopup_Loaded(object sender, RoutedEventArgs e)
		{
			this.mIsPopupLoaded = true;
		}
		public void Hide(DlgReturnEventArg dlgReturnEventArg_0 = null)
		{
			if (!this.mIsShow)
			{
				return;
			}
			if (this.mAppBar != null)
			{
				if (!this.mAppBar.get_IsVisible())
				{
					this.mAppBar.set_IsVisible(true);
				}
				this.mAppBar.set_IsMenuEnabled(this.mIsMenuEnabled);
				int num = 0;
				IEnumerator enumerator = this.mAppBar.get_Buttons().GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						object current = enumerator.get_Current();
						ApplicationBarIconButton applicationBarIconButton = current as ApplicationBarIconButton;
						if (applicationBarIconButton != null)
						{
							if (num < this.mBarButtonEnables.get_Count())
							{
								applicationBarIconButton.set_IsEnabled(this.mBarButtonEnables.get_Item(num++));
							}
							else
							{
								applicationBarIconButton.set_IsEnabled(true);
							}
						}
					}
				}
				finally
				{
					IDisposable disposable = enumerator as IDisposable;
					if (disposable != null)
					{
						disposable.Dispose();
					}
				}
				this.mAppBar = null;
			}
			if (this.mPage != null)
			{
				this.mPage.remove_BackKeyPress(new EventHandler<CancelEventArgs>(this.mPage_BackKeyPress));
				BasePage.setSystemTrayDefault();
				if (this.mPage == MainPage.mainPageInstance)
				{
					MainPage.mainPageInstance.mNBackKeyRelayCount--;
				}
			}
			this.mBarButtonEnables.Clear();
			this.mPopup.set_IsOpen(false);
			this.mPopup.set_Child(null);
			this.mPopup = null;
			if (this.mOverlay != null)
			{
				this.mOverlay.get_Children().Clear();
				this.mOverlay = null;
			}
			if (this.DlgReturnCallBack != null)
			{
				if (dlgReturnEventArg_0 == null)
				{
					dlgReturnEventArg_0 = new DlgReturnEventArg(null);
				}
				this.DlgReturnCallBack(dlgReturnEventArg_0);
			}
			this.mIsShow = false;
		}
		public bool IsShow()
		{
			return this.mIsShow;
		}
	}
}
